//
//  AbnormalReportMainCell.m
//  LogisticsSupplier
//
//  Created by 吕晶 on 2021/3/16.
//  Copyright © 2021 吕晶. All rights reserved.
//

#import "AbnormalReportMainCell.h"
#import "LJCustomTextView.h"
#import "XLPhotoBrowser.h"

@interface AbnormalReportMainCell ()<XLPhotoBrowserDelegate>

@property (nonatomic, strong) UIView *bg1View;
//@property (nonatomic, strong) UIButton *btn1;
//@property (nonatomic, strong) UIButton *btn2;
//@property (nonatomic, strong) UIButton *btn3;
@property (nonatomic, strong) UIView *bg2View;
@property (nonatomic, strong) UILabel *imgTitleLab;
@property (nonatomic, strong) UIView *imgsView;
@property (nonatomic, strong) UILabel *line;
@property (nonatomic, strong) UILabel *fileTitleLab;
@property (nonatomic, strong) UILabel *fileCountLab;
@property (nonatomic, strong) LJCustomTextView *fileTextView;
@property (nonatomic, strong) UIButton *sureBtn;

@end

@implementation AbnormalReportMainCell

+ (instancetype)AbnormalReportMainCellWithTableView:(UITableView *)tableView {
    static NSString *cellID = @"AbnormalReportMainCell";
    AbnormalReportMainCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[AbnormalReportMainCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = Color_backgroundColor;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.bg1View = [UIView new];
        self.bg1View.layer.masksToBounds = YES;
        self.bg1View.layer.cornerRadius = 6;
        self.bg1View.backgroundColor = HexColor(0xffffff);
        [self.contentView addSubview:self.bg1View];
        
//        self.btn1 = [UIButton new];
//        [self.btn1 setTitle:@"包装破损" forState:0];
//        self.btn1.backgroundColor = HexColor(0xF2F2F5);
//        [self.btn1 setTitleColor:HexColor(0xCCCCD9) forState:0];
//        self.btn1.titleLabel.font = [UIFont systemFontOfSize:14];
//        self.btn1.layer.masksToBounds = YES;
//        self.btn1.layer.cornerRadius = 15;
//        [self.btn1 addTarget:self action:@selector(btn1Click) forControlEvents:UIControlEventTouchUpInside];
//        [self.bg1View addSubview:self.btn1];
//
//        self.btn2 = [UIButton new];
//        [self.btn2 setTitle:@"数量有误" forState:0];
//        self.btn2.backgroundColor = HexColor(0xF2F2F5);
//        [self.btn2 setTitleColor:HexColor(0xCCCCD9) forState:0];
//        self.btn2.titleLabel.font = [UIFont systemFontOfSize:14];
//        self.btn2.layer.masksToBounds = YES;
//        self.btn2.layer.cornerRadius = 15;
//        [self.btn2 addTarget:self action:@selector(btn2Click) forControlEvents:UIControlEventTouchUpInside];
//        [self.bg1View addSubview:self.btn2];
//
//        self.btn3 = [UIButton new];
//        [self.btn3 setTitle:@"其他" forState:0];
//        self.btn3.backgroundColor = HexColor(0xF2F2F5);
//        [self.btn3 setTitleColor:HexColor(0xCCCCD9) forState:0];
//        self.btn3.titleLabel.font = [UIFont systemFontOfSize:14];
//        self.btn3.layer.masksToBounds = YES;
//        self.btn3.layer.cornerRadius = 15;
//        [self.btn3 addTarget:self action:@selector(btn3Click) forControlEvents:UIControlEventTouchUpInside];
//        [self.bg1View addSubview:self.btn3];
        
        self.bg2View = [UIView new];
        self.bg2View.layer.masksToBounds = YES;
        self.bg2View.layer.cornerRadius = 6;
        self.bg2View.backgroundColor = HexColor(0xffffff);
        [self.contentView addSubview:self.bg2View];
        
        self.imgTitleLab = [UILabel new];
        self.imgTitleLab.text = @"上传附件";
        self.imgTitleLab.textColor = HexColor(0x2B2C44);
        self.imgTitleLab.font = BoldFontOfSize(18);
        [self.bg2View addSubview:self.imgTitleLab];
        
        self.imgsView = [UIView new];
        [self.bg2View addSubview:self.imgsView];
        
        self.line = [UILabel new];
        self.line.backgroundColor = HexColor(0xF2F2F5);
        [self.bg2View addSubview:self.line];
        
        self.fileTitleLab = [UILabel new];
        self.fileTitleLab.textColor = HexColor(0x2B2C44);
        self.fileTitleLab.font = [UIFont systemFontOfSize:14];
        self.fileTitleLab.text = @"备注";
        [self.bg2View addSubview:self.fileTitleLab];
        
        self.fileCountLab = [UILabel new];
        self.fileCountLab.textColor = HexColor(0xBDBED9);
        self.fileCountLab.font = [UIFont systemFontOfSize:12];
        self.fileCountLab.text = @"0/80";
        [self.bg2View addSubview:self.fileCountLab];
        
        self.fileTextView = [LJCustomTextView new];
        self.fileTextView.placeholder = @"请输入";
        self.fileTextView.textColor = HexColor(0x2B2C44);
        self.fileTextView.font = [UIFont systemFontOfSize:14];
        self.fileTextView.textLength = 80;
        __weak_self;
        self.fileTextView.countBlock = ^(NSString * _Nonnull str) {
            weakself.fileCountLab.text = str;
            if (weakself.remarkBlock) {
                weakself.remarkBlock(weakself.fileTextView.text);
            }
        };
        [self.bg2View addSubview:self.fileTextView];
        
        self.sureBtn = [UIButton new];
        self.sureBtn.layer.masksToBounds = YES;
        self.sureBtn.layer.cornerRadius = 6;
        self.sureBtn.backgroundColor = Color_ThemeColor;
        [self.sureBtn setTitle:@"上报" forState:0];
        [self.sureBtn setTitleColor:HexColor(0xffffff) forState:0];
        self.sureBtn.titleLabel.font = BoldFontOfSize(16);
        [self.sureBtn addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.sureBtn];
    }
    return self;
}

- (void)sureClick {
    if (self.sureBlock) {
        self.sureBlock();
    }
}

-(void)setModel:(AbnormalReportMainModel *)model {
    _model = model;
    [self configUI];
}

- (void)configUI {
    self.bg1View.nim_left = 15;
    self.bg1View.nim_top = 10;
    self.bg1View.nim_width = ScreenW - 30;
    
    [self.bg1View.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    __block CGFloat btnLeft = 20;
    __block CGFloat btnTop = 15;
    CGFloat margin = 10;
    CGFloat btnH = 30;
    __block CGFloat tipH = 0;
    [self.model.typeArray enumerateObjectsUsingBlock:^(AbnormalReportMainTypeModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn = [UIButton new];
        btn.imageView.contentMode = UIViewContentModeScaleAspectFill;
        [btn setTitle:obj.name forState:0];
        btn.backgroundColor = obj.isSelect ? Color_ThemeColor : HexColor(0xF2F2F5);
        [btn setTitleColor:obj.isSelect ? HexColor(0xffffff) : HexColor(0xCCCCD9) forState:0];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = btnH * 0.5;
        btn.tag = idx;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        CGFloat textW = [FactoryMethod sizeWithText:obj.name font:btn.titleLabel.font maxSize:CGSizeMake(0, 0)].width;
        CGFloat btnW = ((textW + 30) > (self.bg1View.nim_width - 2 * 20)) ? (self.bg1View.nim_width - 2 * 20) : (textW + 30);
        btn.nim_left = btnLeft;
        btn.nim_top = btnTop;
        btn.nim_width = btnW;
        btn.nim_height = btnH;
        [self.bg1View addSubview:btn];
        
        if (20 + btn.nim_right > self.bg1View.nim_width) {
            btnTop = btn.nim_bottom + margin;
            btn.nim_left = 20;
            btn.nim_top = btnTop;
        }
        
        btnLeft = btn.nim_right + margin;
        
        tipH = btn.nim_bottom + 15;
    }];
    
    self.bg1View.nim_height = tipH;
    
    self.bg2View.nim_left = self.bg1View.nim_left;
    self.bg2View.nim_top = self.bg1View.nim_bottom + 10;
    self.bg2View.nim_width = self.bg1View.nim_width;
    
    [self.imgTitleLab sizeToFit];
    self.imgTitleLab.nim_left = 15;
    self.imgTitleLab.nim_top = 20;
    
    self.imgsView.frame = CGRectMake(15, 60, self.bg2View.nim_width - 30, 90);
    CGFloat imgW = (self.imgsView.nim_width - 40) * 0.33333;
    self.imgsView.nim_height = imgW + 10;
    __block CGFloat viewLeft = 0;
    [self.imgsView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.model.imgList enumerateObjectsUsingBlock:^(AbnormalReportImgModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn = [UIButton new];
        btn.imageView.contentMode = UIViewContentModeScaleAspectFill;
        btn.nim_left = (imgW + 20) * idx;
        btn.nim_top = 10;
        btn.nim_width = imgW;
        btn.nim_height = imgW;
        [btn sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",obj.url,obj.path]] forState:0];
        btn.tag = idx;
        [btn addTarget:self action:@selector(imgPreView:) forControlEvents:UIControlEventTouchUpInside];
        [self.imgsView addSubview:btn];
        
        UIButton *deleteBtn = [[UIButton alloc] initWithFrame:CGRectMake(btn.nim_right - 15, btn.nim_top - 7, 22, 22)];
        [deleteBtn setImage:[UIImage imageNamed:@"icon_caozuodan_delete_img"] forState:0];
        deleteBtn.tag = idx;
        [deleteBtn addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.imgsView addSubview:deleteBtn];
        
        viewLeft = btn.nim_right + 20;
    }];
    
    if (self.model.imgList.count < 3) {
        UIButton *addBtn = [[UIButton alloc] initWithFrame:CGRectMake(viewLeft, 10, imgW, imgW)];
        [addBtn setBackgroundImage:[UIImage imageNamed:@"icon_caozuodan_upload_img"] forState:0];
        [addBtn addTarget:self action:@selector(addClick) forControlEvents:UIControlEventTouchUpInside];
        [self.imgsView addSubview:addBtn];
    }
    
    self.line.frame = CGRectMake(15, self.imgsView.nim_bottom + 10, self.bg2View.nim_width - 30, 1);
    
    self.fileTitleLab.nim_left = 15;
    [self.fileTitleLab sizeToFit];
    self.fileTitleLab.nim_top = self.line.nim_bottom + 20;
    
    self.fileCountLab.nim_left = self.fileTitleLab.nim_right + 10;
    [self.fileCountLab sizeToFit];
    self.fileCountLab.nim_bottom = self.fileTitleLab.nim_bottom;
    self.fileCountLab.nim_width += 50;
    
    self.fileTextView.frame = CGRectMake(self.fileTitleLab.nim_left, self.fileTitleLab.nim_bottom + 10, self.bg2View.nim_width - 2 * self.fileTitleLab.nim_left, 80);
    
    self.fileTextView.customText = self.model.note.length?self.model.note:@"";
    
    self.bg2View.nim_height = self.fileTextView.nim_bottom + 15;
    
    self.sureBtn.nim_left = self.bg1View.nim_left;
    self.sureBtn.nim_top = self.bg2View.nim_bottom + 20;
    self.sureBtn.nim_width = self.bg1View.nim_width;
    self.sureBtn.nim_height = 44;
    
    self.height = self.sureBtn.nim_bottom + 10;
}

- (void)btnClick:(UIButton *)sender {
    if (self.changeBlock) {
        self.changeBlock(sender.tag);
    }
}

- (void)imgPreView:(UIButton *)sender {
    NSMutableArray *mut = [NSMutableArray array];
    [self.model.imgList enumerateObjectsUsingBlock:^(AbnormalReportImgModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [mut addObject:[NSString stringWithFormat:@"%@%@",obj.url,obj.path]];
    }];
    XLPhotoBrowser *browser = [XLPhotoBrowser showPhotoBrowserWithImages:mut currentImageIndex:sender.tag];
    browser.browserStyle = XLPhotoBrowserStyleIndexLabel;//微博样式
    [browser setActionSheetWithTitle:nil delegate:self cancelButtonTitle:nil deleteButtonTitle:nil otherButtonTitles:@"保存图片", nil];
}

- (void)photoBrowser:(XLPhotoBrowser *)browser clickActionSheetIndex:(NSInteger)actionSheetindex currentImageIndex:(NSInteger)currentImageIndex {
    [browser saveCurrentShowImage];
}

- (void)deleteClick:(UIButton *)sender {
    if (self.deleteBlock) {
        self.deleteBlock(sender.tag);
    }
}

- (void)addClick {
    if (self.addBlock) {
        self.addBlock();
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
